/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.perekamanitembiaya;

import org.sadhar.sipp.posbiaya.PosBiayaDAO;
import org.sadhar.sipp.posbiaya.PosBiayaDAOImpl;
import org.sadhar.sipp.satuan.SatuanDAO;
import org.sadhar.sipp.satuan.SatuanDAOImpl;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Bandbox;
import org.zkoss.zul.Button;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Row;
import org.zkoss.zul.RowRenderer;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Window;

/**
 *
 * @author hendro
 */
public class ItemBiayaRowRenderer implements RowRenderer {

    private PosBiayaDAO posBiayaDAO = new PosBiayaDAOImpl();
    private SatuanDAO satuanDAO = new SatuanDAOImpl();
    private PerekamanItemBiayaDAO itemDAO = new PerekamanItemBiayaDAOImpl();

    public void render(final Row row, Object data) throws Exception {
        final PerekamanItemBiaya item = (PerekamanItemBiaya) data;
        new Label(posBiayaDAO.select(item.getKodePosBiaya()).getNamaPosBiaya()).setParent(row);
        new Label(item.getNamaItem()).setParent(row);

        Toolbarbutton toolbarbtnUbah = new Toolbarbutton();
        toolbarbtnUbah.setLabel("[ubah]");
        toolbarbtnUbah.addEventListener("onClick", new EventListener() {

            public void onEvent(Event arg0) throws Exception {
                Window window = (Window) row.getRoot().getFellow("contentDiv").getFirstChild();
                Label lblPosBiaya = (Label) window.getFellow("lblPosBiaya");
                lblPosBiaya.setValue(posBiayaDAO.select(item.getKodePosBiaya()).getNamaPosBiaya());
                Textbox txtboxKodePosBiaya = (Textbox) window.getFellow("txtboxKodePosBiaya");
                txtboxKodePosBiaya.setValue(item.getKodePosBiaya() + "");
                Textbox txtboxNamaItemBiaya = (Textbox) window.getFellow("txtboxNamaItemBiaya");
                txtboxNamaItemBiaya.setValue(item.getNamaItem());
                Textbox txtboxKodeItemBiaya = (Textbox) window.getFellow("txtboxKodeItemBiaya");
                txtboxKodeItemBiaya.setValue(item.getKodeItem() + "");                
                Textbox txtboxCariSatuanBarang = (Textbox)window.getFellow("txtboxCariSatuanBarang");
                txtboxCariSatuanBarang.setValue(satuanDAO.select(item.getKodeSatuan()).getSatuan());
                Textbox txtboxKodeSatuanBarang = (Textbox) window.getFellow("txtboxKodeSatuanBarang");
                txtboxKodeSatuanBarang.setValue(item.getKodeSatuan() + "");
            }
        });

        final Toolbarbutton toolbarbtnHapus = new Toolbarbutton();
        toolbarbtnHapus.setLabel("[hapus]");
        toolbarbtnHapus.addEventListener("onClick", new EventListener() {

            public void onEvent(Event arg0) throws Exception {
                if (Messagebox.show("Apakah data Item Biaya: " + item.getNamaItem() + " akan dihapus?", "Konfirmasi", Messagebox.YES | Messagebox.NO, null) == Messagebox.YES) {
                    try {
                        itemDAO.delete(item);
                    } catch (Exception ex) {
                        Messagebox.show("Gagal menghapus data: " + ex.getMessage());
                    }
                    Window window = (Window) row.getRoot().getFellow("contentDiv").getFirstChild();
                    Button btnBatal = (Button) window.getFellow("btnBatal");
                    toolbarbtnHapus.addForward(null, btnBatal, "onClick");
                }
            }
        });

        
        Hbox box = new Hbox();
        box.appendChild(toolbarbtnUbah);
        box.appendChild(toolbarbtnHapus);
        box.setParent(row);
    }
}
